package com.ruoyi.order.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.order.domain.JsonData;
import com.ruoyi.order.enums.BizCodeEnum;
import com.ruoyi.order.feign.ProductFeignService;
import com.ruoyi.order.request.ConfirmOrderRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.service.IOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单主表Controller
 *
 * @author 林道敬
 * @date 2025-01-04
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ProductFeignService productFeignService;

    @PostMapping("confirm")
    public R confirmOrder(@RequestBody ConfirmOrderRequest orderRequest, HttpServletResponse response ){
        System.out.println("orderRequest :"+orderRequest);
        R result = orderService.confirmOrder(orderRequest);
        return result;
    }

    @GetMapping("/pay/{out_trade_no}")
    public JsonData payOrder(@PathVariable("out_trade_no")String outTradeNo){
        //修改值
        Order order = new Order();
        order.setState("PAY");
        UpdateWrapper<Order> orderUpdateWrapper = new UpdateWrapper<>();
        orderUpdateWrapper.eq("out_trade_no", outTradeNo);
        orderService.update(order, orderUpdateWrapper);
        return JsonData.buildSuccess();
    }
    @GetMapping("/dispay/{out_trade_no}")
    public JsonData dsipayOrder(@PathVariable("out_trade_no")String outTradeNo){
        //修改值
        Order order = new Order();
        order.setState("CANCEL");
        UpdateWrapper<Order> orderUpdateWrapper = new UpdateWrapper<>();
        orderUpdateWrapper.eq("out_trade_no", outTradeNo);
        orderService.update(order, orderUpdateWrapper);
        return JsonData.buildSuccess();
    }

    @GetMapping("/query_state/{out_trade_no}")
    public JsonData queryProductOrderState(@PathVariable("out_trade_no")String outTradeNo) {

        String state = orderService.queryProductOrderState(outTradeNo);

        return StringUtils.isBlank(state) ? JsonData.buildResult(BizCodeEnum.ORDER_CONFIRM_NOT_EXIST) : JsonData.buildSuccess(state);
    }
        /**
         * 查询订单主表列表
         */
    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单主表列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单主表数据");
    }

    /**
     * 获取订单主表详细信息
     */
    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderService.selectOrderById(id));
    }

    /**
     * 新增订单主表
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "订单主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单主表
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "订单主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单主表
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }
}
